<?php
/**
 * Created by 荣耀电竞.
 * User: 林子彦 <278805354@qq.com>
 * Date: 2018/7/10 0010
 * Time: 17:12
 */

namespace App\Ordersys\Models;

use App\Admin\Models\MemberModel;
use App\Base\Models\BaseModel;
use Illuminate\Database\Eloquent\SoftDeletes;

class AwardLogModel extends BaseModel
{
    use SoftDeletes;
    /**
     * 订单产品
     * @var string
     */
    public $table = "award_log";
    /**
     * 时间自动
     * @var bool
     */
    public $timestamps = true;
    /**
     * The name of the "created at" column.
     *
     * @var string
     */
    const CREATED_AT = 'create_time';  //修改添加时间字段
    /**
     * The name of the "updated at" column.
     *
     * @var string
     */
    const UPDATED_AT = 'update_time';

    /**
     * 删除
     */
    const DELETED_AT = 'delete_time';
    /**
     * 模型的日期字段保存格式。
     */
    protected $dateFormat = 'U';
   protected $fillable = ['award_type'];
    /**
     * 设置模型
     * @return LuckDrawModel
     */
    public function getLuckDrawModel()
    {
        return app()->make(LuckDrawModel::class);
    }

    /**
     *
     * @return MemberModel
     */
    public function getMemberModel()
    {
        return app()->make(MemberModel::class);
    }

    /**
     * 金额操作
     * @param $type
     * @param array $data
     * @throws OrdersysException
     */
    public function awardLog($type, $data = array())
    {

        $stLuck = $this->getLuckDrawModel()->newQuery()->where("id", 1)->first();

        if (empty($stLuck)) {
            return false;
        }

        switch ($type) {
            case 1:
                $set = $stLuck["goods_num"];
                $set_arr = explode("/", $set);
                if ($data['money'] > $set_arr[0]) {
                    $anum = intval($data["money"] / $set_arr[0]) * $set_arr[1];
                    $this->getMemberModel()->newQuery()->where("uid", $data["uid"])->increment("award_num", $anum);
                    $adata['anum'] = $anum;
                    $data["remark"] = $data["remark"] . $set_arr[0];
                    $this->newQuery()->create($data);
                }
                break;
            case 2:
                $set = $stLuck["artitle_sum"];
                $set_arr = explode("/", $set);
                $memberInfo=$this->getMemberModel()->newQuery()->where("uid",$data["uid"])->first();
                if ($memberInfo->aritcle_num > $set_arr[0]) {
                    $this->getMemberModel()->newQuery()->where("uid", $data["uid"])->increment("award_num", $set_arr[1]);
                    $adata['anum'] = 1;
                    $data["remark"] = $data["remark"] . $set_arr[0];
                    $this->newQuery()->create($data);
                    $this->getMemberModel()->newQuery()->where("uid", $data["uid"])->update(['aritcle_num'=>0]);
                }
                break;
        }
        return true;


    }

}
